<?php

/**
 * @author: luoxw@surekam.com
 */

class GoogleTranslator {
	private $message = "";

	private $url = "http://translate.google.cn/";

	private $text = ""; //翻译文本
	private $fields;
	public function run() {
		$p = '/@(\S+)\s+(.*)/';
		preg_match($p, trim($_POST[Message]), $m);
		if (trim($m[2]) != "") {
			$funName = "__" . trim($m[1]);
			if (method_exists($this, $funName)) {
				$this->text = $m[2];
				$this-> $funName ($m);
			} else {
				$this->message = "还没提供词种翻译类型- -";
			}

		} else {
			$this->message = "参数有误-。-";
		};
	}

	private function translate() {

		$gphtml = $this->postPage();
		//提取翻译结果
		$p = '/<span[^>]*>([^>]+)<\/span>/';
		preg_match_all($p, $gphtml, $m);
		$out = $m[1][0];


		$this->message =$out;

	}

	private function postPage() {

			$ch = curl_init($this->url);

			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

			curl_setopt($ch, CURLOPT_HEADER, 0);

			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

			curl_setopt($ch, CURLOPT_TIMEOUT, 15);

			/*
			
			 *hl – 界面语言，此处无用。
			
			*langpair – src lang to dest lang
			
			 *ie – urlencode的编码方式?
			
			 *text – 要翻译的文本
			
			
			if ($this->toE == "toE") {
				$fields = array (
					'langpair=auto|en',
					'ie=UTF-8',
					'text=' . urlencode($text)
				);
			}
			elseif ($this->toE == "toC") {
				$fields = array (
					'langpair=auto|zh-CN',
					'ie=UTF-8',
					'text=' . $text
				);
			}
			elseif ($this->toE == "toJ") {
				$fields = array (
					'langpair=auto|ja',
					'ie=UTF-8',
					'text=' . $text
				);
			}
			elseif ($this->toE == "toF") {
				$fields = array (
					'langpair=auto|fr',
					'ie=UTF-8',
					'text=' . $text
				);
			}
			elseif ($this->toE == "toK") {
				$fields = array (
					'langpair=auto|ko',
					'ie=UTF-8',
					'text=' . $text
				);
			}
			*/
			curl_setopt($ch, CURLOPT_POST, 1);

			curl_setopt($ch, CURLOPT_POSTFIELDS, implode('&', $this->fields));

			$html = curl_exec($ch);
			if (curl_errno($ch))
				$html = "";

			curl_close($ch);

		

		return mb_convert_encoding($html, 'UTF-8', 'GB2312');

	}

	private function __toE() {
		$this->fields = array (
					'langpair=auto|en',
					'ie=UTF-8',
					'text=' . urlencode($this->text)
				);
		$this->translate();
	}
	
	
	
	private function __toC() {
				$this->fields = array (
					'langpair=auto|zh-CN',
					'ie=UTF-8',
					'text=' . urlencode($this->text)
				);
				
				$this->translate();
	}
	
	private function __toJ() {
			$this->fields = array (
					'langpair=zh-CN|ja',
					'ie=UTF-8',
					'text=' . urlencode($this->text)
				);
				
				$this->translate();
		
	}

	private function __toF() {
			$this->fields = array (
					'langpair=auto|fr',
					'ie=UTF-8',
					'text=' . urlencode($this->text)
				);
				
				$this->translate();
		
	}

	private function __toD() {
			$this->fields = array (
					'langpair=auto|de',
					'ie=UTF-8',
					'text=' . urlencode($this->text)
				);
				
				$this->translate();
		
	}
	private function __get($property_name) {
		return $this-> $property_name;
	}

	private function __set($property_name, $value) {
		$this-> $property_name = $value;
	}

}
?>